package cn.zjnu.service.impl;

import cn.zjnu.dao.ShoppingDao;
import cn.zjnu.domain.Cart;
import cn.zjnu.domain.Goods;
import cn.zjnu.domain.User;
import cn.zjnu.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingServiceImpl implements ShoppingService {
    @Autowired
    private ShoppingDao shoppingDao;
    @Override
    public List<Goods> showGoodsMsg() {
       return shoppingDao.getAll();
    }

    @Override
    public List<Goods> showGoodsMsgById(Integer id) {
        return shoppingDao.getAllById(id);
    }

    @Override
    public boolean addGoods(Cart cart) {
        System.out.println("ShoppingService get cart : " + cart);
        if(cart.getSp_amount() == 0){//新建一条数据
            cart.setSp_amount(1);
            return shoppingDao.add(cart) > 0;
        }else{//数量+1
            int amount = cart.getSp_amount() + 1;
            double total_price = amount * cart.getSp_price();
            cart.setSp_amount(amount);
            cart.setTotal_price(total_price);
            return shoppingDao.update(cart) > 0;
        }
    }

    @Override
    public boolean plusGoods(Cart cart) {
        System.out.println("service plus goods ...");
        int amount = cart.getSp_amount() + 1;
        double total_price = amount * cart.getSp_price();
        cart.setSp_amount(amount);
        cart.setTotal_price(total_price);
        return  shoppingDao.update(cart) > 0;
    }

    @Override
    public boolean reduceGoods(Cart cart) {
        System.out.println("service reduce goods ...");
        int amount = cart.getSp_amount() - 1;
        double total_price = amount * cart.getSp_price();
        if(amount == 0){
            return false;
        }else{
            cart.setSp_amount(amount);
            cart.setTotal_price(total_price);
            return  shoppingDao.update(cart) > 0;
        }
    }

    @Override
    public boolean deleteGoodsInCart(Cart cart) {
        System.out.println("service delete goods ...");
        return shoppingDao.delete(cart) > 0;
    }

    public Double showAccount(Integer user_id){
        Double total_money = 0.0;
        List<Cart> cartList = shoppingDao.getCartByUser(user_id);
        System.out.println("cartList:" + cartList);
//        int i = 0;
//        while(cartList.get(i)!=null){
//            ++i;
//        }
        for(int i = 0 ; i < cartList.size() ; i ++ ){
            total_money += cartList.get(i).getTotal_price();
            System.out.println("total money = " + total_money + ", and " + cartList.get(i).getSp_name()+ " price is " +cartList.get(i).getTotal_price());

        }
        return total_money;
    }
    @Override
    public boolean settleAccount(Integer id) {
        /*
            流程
                1. 查询用户的余额
                2. 扣除结算的金额
                3. 清空购物车
         */
        Double user_money = 0.0;
        Double cost_money = 0.0;
        Double account = 0.0;
        User user = shoppingDao.getUserById(id);
        System.out.println("user msg:" + user);
        user_money = user.getMoney();
        cost_money = showAccount(id);
        account = user_money - cost_money;
        if(account >= 0 ){
            //能够支付
            user.setMoney(account);
            shoppingDao.updateUser(user);//修改用户的账户余额
            shoppingDao.deleteAllById(id);//清空用户的购物车
            return true;
        }
        return false;//不足以支付
    }

}
